<template>
	<el-card>
		<div class="accessDivClass">
			<el-form :inline="true" :model="state.queryForm">
				<el-form-item>
					<el-input v-model="state.queryForm.standardNameOrNo" placeholder="标准名称/标准号搜索"></el-input>
				</el-form-item>
				<el-form-item>
					<fast-select v-model="state.queryForm.standardGroup" dict-type="file_standard_type" placeholder="标准分类" clearable></fast-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="state.queryForm.status" placeholder="标准状态" clearable>
						<el-option :value="'1'" label="现行"></el-option>
						<el-option :value="'0'" label="废止"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button @click="getDataList" type="primary" :icon="Search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button :icon="Refresh" @click="resets">重置</el-button>
				</el-form-item>
			</el-form>
			<el-table
				v-loading="state.dataListLoading"
				:data="state.dataList"
				border
				style="width: 100%"
				max-height="calc(100vh - 340px )"
				@selection-change="selectionChangeHandle"
			>
				<!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
				<el-table-column prop="name" label="标准名称" header-align="center" align="center"></el-table-column>
				<el-table-column prop="standardNo" label="标准号" header-align="center" align="center"></el-table-column>
				<fast-table-column
					prop="standardGroup"
					label="标准分类"
					header-align="center"
					align="center"
					dictType="file_standard_type"
				></fast-table-column>
				<el-table-column prop="status" label="状态" header-align="center" align="center">
					<template #default="scope">
						<span v-if="scope.row.status == 1">现行</span>
						<span v-else>废止</span>
					</template>
				</el-table-column>
				<el-table-column prop="issuingDate" label="发布日期" header-align="center" align="center">
					<template #default="scope">
						{{ formaterDate(scope.row.issuingDate) }}
					</template>
				</el-table-column>
				<el-table-column prop="implementationDate" label="实施日期" header-align="center" align="center">
					<template #default="scope">
						{{ formaterDate(scope.row.implementationDate) }}
					</template>
				</el-table-column>
				<el-table-column prop="collectionTime" label="收录时间" header-align="center" align="center">
					<template #default="scope">
						{{ formaterDate(scope.row.collectionTime) }}
					</template>
				</el-table-column>
				<el-table-column prop="downloadCount" label="下载次数" header-align="center" align="center"></el-table-column>
				<el-table-column prop="createTime" label="操作" header-align="center" align="center" width="165">
					<template #default="scope">
						<el-button type="primary" link @click="fileSee(scope.row.fileUrl, scope.row.type, scope.row.name, scope.row.id)">预览</el-button>
						<el-button type="primary" link @click="downFile(scope.row.fileUrl, scope.row.name, scope.row.id)">下载</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				:current-page="state.page"
				:page-sizes="state.pageSizes"
				:page-size="state.limit"
				:total="state.total"
				layout="total, sizes, prev, pager, next, jumper"
				@size-change="sizeChangeHandle"
				@current-change="currentChangeHandle"
			>
			</el-pagination>

			<div class="drawerClass" style="height: 100%">
				<iframe-file ref="iframeFileRef" @downFile="getDataList"></iframe-file>
			</div>
		</div>
	</el-card>
</template>

<script setup lang="ts">
import { Delete, Edit, Search, Plus, CircleClose, Refresh } from '@element-plus/icons-vue'
import { useCrud } from '@/hooks'
import { useCrudFiles } from '@/hooks/indexFilesPdf'
import { nextTick, reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import timeFormater from 'time-formater'
import { addIncreaseDownload } from '@/api/standard-services/index'
import IframeFile from '@/components/iframe-file/index.vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/data-governance/standard-file/page',
	queryForm: {
		standardNameOrNo: '',
		status: '',
		standardGroup: ''
	}
})

const formaterDate = data => {
	if (data) {
		return timeFormater(data).format('YYYY-MM-DD')
	} else {
		return ''
	}
}

const resets = () => {
	state.queryForm = {
		standardNameOrNo: '',
		status: '',
		standardGroup: ''
	}
	state.page = 1
	state.limit = 10

	getDataList()
}

const downFile = (url, name, id) => {
	if (url) {
		downloadHandle(url, name)
		addIncreaseDownload(id).then(() => {
			getDataList()
		})
	}
}

const iframeFileRef = ref()
const fileSee = (url: any, type: any, name, id) => {
	nextTick(() => {
		iframeFileRef.value.init(url, type, name, id)
	})
}
const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle } = useCrud(state)
const { downloadHandle } = useCrudFiles({})
</script>
<style lang="scss">
.accessDivClass {
	height: calc(100vh - 170px);
	position: relative;
	overflow: hidden;
}
.accessDivClass .drawerClass .el-overlay {
	height: 100%;
	position: absolute !important;
	overflow: hidden;
	width: 100%;

	.el-drawer__body {
		overflow: hidden !important;
	}
}
</style>
